package com.ultrasoft.farmasoft.repository;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import javax.persistence.TypedQuery;

import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import com.ultrasoft.farmasoft.domain.Credititem;

@Repository(value = "creditItemDao")
@Transactional(readOnly = false)
public class CreditItemDaoImpl implements CreditItemDao {

	private EntityManager em = null;
	@PersistenceContext
	public void setEntityManager(EntityManager em) {
		this.em = em;
	}
	public List<Credititem> getListCreditItem() {
		TypedQuery<Credititem> q = em.createNamedQuery("Credititem.findAll",Credititem.class);
    	return q.getResultList();
	}

	public List<Credititem> getListCreditItemByAccountReceivableId(Integer accountReceivableId) {
		TypedQuery<Credititem> q = em.createNamedQuery("Credititem.findByAccountReceivableId",Credititem.class);
    	q.setParameter("accountReceivableId",accountReceivableId);
    	return q.getResultList();
	}

	public List<Credititem> getListCreditItemByPackageId(Integer packageId) {
		TypedQuery<Credititem> q = em.createNamedQuery("Credititem.findByPackageId",Credititem.class);
    	q.setParameter("packageId",packageId);
    	return q.getResultList();
	}

	public List<Credititem> getListCreditItemByItemId(Integer itemId) {
		TypedQuery<Credititem> q = em.createNamedQuery("Credititem.findByItemId",Credititem.class);
    	q.setParameter("itemId",itemId);
    	return q.getResultList();
	}

	public List<Credititem> getListCreditItemByPackageIdByItemId(Integer packageId, Integer itemId) {
		TypedQuery<Credititem> q = em.createNamedQuery("Credititem.findByPackageIdByItemId",Credititem.class);
    	q.setParameter("packageId",packageId);
    	q.setParameter("itemId",itemId);
    	return q.getResultList();
	}

	public Credititem findCreditItemByAccountReceivableIdByPackageIdByItemId(Integer accountReceivableId, Integer packageId, Integer itemId) {
		Credititem res = null;
    	Query  q = em.createNamedQuery("Credititem.findByAccountReceivableIdByPackageIdByItemId");
    	q.setParameter("accountReceivableId",accountReceivableId);
    	q.setParameter("packageId",packageId);
    	q.setParameter("itemId",itemId);
    	if(q.getResultList().size()==1)
    		res=(Credititem)q.getSingleResult();
    	return res;
	}

	public void remove(Credititem creditItem) {
		Query q = em.createNamedQuery("Credititem.deleteByAccountReceivableIdByPackageIdByItemId");
		q.setParameter("accountReceivableId",creditItem.getCredititemPK().getAccountReceivableId());
    	q.setParameter("packageId",creditItem.getCredititemPK().getPackageId());
    	q.setParameter("itemId",creditItem.getCredititemPK().getItemId());
		q.executeUpdate();
	}

	public void update(Credititem creditItem) {
		em.merge(creditItem);
	}

	public void add(Credititem creditItem) {
		em.persist(creditItem);
	}

}
